/**
 * ext-override.js
 * contains many overrides to accomodate deficiencies in the basic Ext library
 */

/***
 * Ext.form.FieldSet
 * @author Chris Scott
 * Override Ext.form.FieldSet to add enable/disable functionality to child-fields to improve form-validation
 *
 */
Ext.override(Ext.Panel, {

    /***
     * disableFields
     * queries this component recursively for Ext.form.Fileds.  disables them.
     * @author Chris Scott
     */
    disableFields : function() {
        var fields = this.findBy(function(i) {
            return (i instanceof Ext.form.Field) ? true : false;
        });
        //console.log('disableFields: ', fields);
        for (var n=0,len=fields.length;n<len;n++) {
            fields[n].disable();
        }
    },

    /***
     * enableFields
     * queries this component for Ext.form.Fields.  enables them.
     * @author Chris Scott
     */
    enableFields : function() {
        var fields = this.findBy(function(i) {
            return (i instanceof Ext.form.Field) ? true : false;
        });
        //console.log('enableFields: ', fields);
        for (var n=0,len=fields.length;n<len;n++) {
            fields[n].enable();
        }
    },

    /**
     * Disable this component.
     * @param disableFields [false]
     * @return {Ext.Component} this
     * @author Chris Scott
     */
    disable : function(disableFields){
        disableFields = disableFields || false;
        if (disableFields == true) {
            this.disableFields();
        }
        if(this.rendered){
            this.onDisable();

        }
        this.disabled = true;
        this.fireEvent("disable", this);
        return this;
    },



    /**
     * Enable this component.
     * @param {Boolean} enableFields [false]
     * @return {Ext.Component} this
     * @author Chris Scott
     */
    enable : function(enableFields){
        enableFields = enableFields || false;
        if (enableFields == true) {
            this.enableFields();
        }
        if(this.rendered){
            this.onEnable();
        }
        this.disabled = false;
        this.fireEvent("enable", this);
        return this;
    }

});

/***
 * Ext.form.FieldSet
 * override onCollapse / onExpand to disable/enable child form-fields
 * @author: Chris Scott
 *
 */
Ext.override(Ext.form.FieldSet, {

    /***
     * onCollapse
     * disable child-fields when this is a checkbox toggle
     * @param {Object} doAnim
     * @param {Object} animArg
     */
    onCollapse : function(doAnim, animArg){
        if(this.checkbox){
            this.checkbox.dom.checked = false;
            this.disableFields();
        }
        this.afterCollapse();
    },

    /***
     * onExpand
     * enable child fields when this is checkBoxToggle
     * @param {Object} doAnim
     * @param {Object} animArg
     */
    onExpand : function(doAnim, animArg){
        if(this.checkbox){
            this.checkbox.dom.checked = true;
            this.enableFields();
        }
        this.afterExpand();
    }
});

Ext.MessageBox = Ext.apply(Ext.MessageBox, {
    fade : function(duration) {
        this.getDialog().hide();
        return this;
    }
});

/***
 * Override
 * add a method to get selected row from a EditorGrid
 *
 */
/* disable for use with BQ
Ext.override(Ext.grid.CellSelectionModel, {
    getSelected: function() {
        if (this.selection) {
            return this.selection.record;
        }
    }
});
*/

/***
 * Ext.form.ComboBox::onEnable
 * ComboBox has no implementation of onEnable.  when a combo is enabled and it uses a hiddenField,
 * the hiddenField will remain disabled.
 *
 * @author Chris Scott
 */
Ext.override(Ext.form.ComboBox, {
    onEnable : function() {
        Ext.form.ComboBox.superclass.onEnable.apply(this, arguments);
        if(this.hiddenField){
            this.hiddenField.disabled = false;
        }
    },
    /***
     * restrictHeight
     * bug in ext-2.0.2 assumes shadow exists on Combo
     */
    restrictHeight : function(){
        this.innerList.dom.style.height = '';
        var inner = this.innerList.dom;
        var pad = this.list.getFrameWidth('tb')+(this.resizable?this.handleHeight:0)+this.assetHeight;
        var h = Math.max(inner.clientHeight, inner.offsetHeight, inner.scrollHeight);
        var ha = this.getPosition()[1]-Ext.getBody().getScroll().top;
        var hb = Ext.lib.Dom.getViewHeight()-ha-this.getSize().height;
        var space = Math.max(ha, hb, this.minHeight || 0)-pad-2;

        /** BUG FOX FOR SHADOW **/
        if (this.shadow === true) { space-=this.list.shadow.offset; }

        h = Math.min(h, space, this.maxHeight);

        this.innerList.setHeight(h);
        this.list.beginUpdate();
        this.list.setHeight(h+pad);
        this.list.alignTo(this.el, this.listAlign);
        this.list.endUpdate();
    }
});

/***
 * Ext.form.Radio fix
 * 2008-2-28
 * http://extjs.com/forum/showthread.php?p=112848#post112848
 *
 */
/*
Ext.override(Ext.form.Radio, {
    getGroupValue : function(){
        var p = this.el.up('form') || Ext.getBody();
        return p.child('input[name='+this.el.dom.name+']:checked', true).value;
    },

    // private
    onClick : function(){
        if(this.el.dom.checked != this.checked){
            var p = this.el.up('form') || Ext.getBody();
            var els = p.select('input[name='+this.el.dom.name+']');
            els.each(function(el){
                if(el.dom.id == this.id){
                    this.setValue(true);
                }else{
                    Ext.getCmp(el.dom.id).setValue(false);
                }
            }, this);
        }
    }

});
*/

/**
 * Ext.form.FormPanel override
 * makes FormPanel able to contain a BorderLayout!!
 * @see Animal: http://extjs.com/forum/showthread.php?t=28125
 */
Ext.override(Ext.form.FormPanel, {
    initComponent: function() {
        this.form = this.createForm();
        this.bodyCfg = {
            tag: 'form',
            cls: this.baseCls + '-body',
            method: this.method || 'POST',
            id: this.formId || Ext.id()
        };
        if(this.fileUpload) {
            this.bodyCfg.enctype = 'multipart/form-data';
        }
        Ext.FormPanel.superclass.initComponent.call(this);
        this.addEvents(
            'clientvalidation'
        );
        this.relayEvents(this.form, ['beforeaction', 'actionfailed', 'actioncomplete']);
    },
    onRender: function(ct, position){
        this.initFields();
        Ext.FormPanel.superclass.onRender.call(this, ct, position);
        this.form.initEl(this.body);
    }
});
